// logs.js
// const util = require('../../utils/util.js')
const client=require('../../utils/client.js')
const server=require('../../utils/server.js')

Page({
  data: {
    zx:'',
    xzList:['灵溪镇','钱库镇'],
    cs:'',
    csList:['A社区','B社区'],
    type:'',
    typeList: ['楼栋单元','民房门牌','卡点'],
    xq:'',
    xqList: ['张三小区','李四小区'],
    dong:'',
    dongList: ['1幢','2幢','3幢'],
    shi:'',
    shiList: ['101室','102室','103室'],
    lu:'',
    luList: ['人民路','102室','103室'],
    hao:'',
    haoList: ['101号','102号','103号'],
    kd:'',
    kdList:['卡点1','卡点2'],
    list:[],
    total:0,
    ndata:[],
    isShowMohu:false,
    isShowJingzhun:false,
    isCS:false,
    isType:false,
    isXq:false,
    isDong:false,
    isShi:false,
    isLu:false,
    isHao:false,
    isKd:false,
    searchText:'',
    all:[]
  },
  onLoad() {
    let _that=this;
    this.setData({server:server.baseUrl})
    client.request('/app/wx/allData','get',{},(d)=>{
      let data=d.data;
      let ddd='';
      if(data.length<1){
        return;
      }
      let aa=[];
      let da;
      let total=0;
      let index=0;
      let user=server.getUserToken().name;
      this.setData({ndata:data});

      data.forEach((e,i)=>{
          let add=_that.getAddr(e);
          if(ddd!==add){
            ddd=add;
            da={id:total,name:add,doorNo:e.address2,num:1,checkUser:user,ids:[e]}
            aa.push(da);
            index++;
          }else{
            aa[index-1].num=aa[index-1].num+1;
            aa[index-1].num=aa[index-1].ids.push(e);
          }
          total++;
      });
      let all= [];
      for (var item in aa) all[item] = aa[item];

      this.setData({
        list:aa,
        all:all,
        total:total
      })
    });
  },
  getAddr(e){
    if(e.addressType=='1'){
      return e.xz+e.cj+e.address1+e.address2+e.address3+e.unit;
    }else if(e.addressType=='2'){
      return e.xz+e.cj+e.address1+e.address2;
    }else if(e.addressType=='3'){
      return  e.xz+e.cj+e.address1;
    }else{
      return '';
    }
  },
   
  initDetailAdress() {
    this.setData({
      isXq:false,
      isDong:false,
      isShi:false,
      isKd:false,
      type:''
    })
  },
  initType2() {
    this.setData({
      isXq:false,
      isDong:false,
      isShi:false,
      isLu:false,
      isHao:false,
      xq:''
    })
  },
  initType1() {
    this.setData({
      isXq:false,
      isDong:false,
      isShi:false,
      isKd:false,
      lu:''
    })
  },
  initType0() {
    this.setData({
      isLu:false,
      isHao:false,
      isKd:false,
      kd:''
    })
  },
  clickMohuBtn() {
    this.setData({
      isShowMohu:true,
      isShowJingzhun:false
    })
  },
  clickJingzhunBtn() {
    this.setData({
      isShowMohu:false,
      isShowJingzhun:true
    })
  },
  changeSearchText(e) {
    let value=e.detail.value;
    let aa={};
    for(let i=0;i<value.length;i++){
        this.data.all.forEach((e)=>{
          console.log(value+"--"+e.name);
          if((e.name+''+e.doorNo).indexOf(value)>-1){
            aa[e.id]=e;
          }
      });
    }
    let ndata=[];
    Object.keys(aa).forEach(function(key){
      ndata.push(aa[key])
    });

    if(value==''){
      ndata = this.data.all
    }
 
    this.setData({
      searchText: e.detail.value,
      list:ndata
    })
  },
  bindXzChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    let name=this.data.xzList[e.detail.value];
 
    this.setData({
      xz: name,
      isCS:true
    })
  },
  bindCSChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      cs: this.data.csList[e.detail.value],
      isType:true
    })
    this.initDetailAdress()
  },
  bindTypeChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({type: this.data.typeList[e.detail.value]})
    if(e.detail.value==0){
      this.setData({
        isXq:true
      })
      this.initType0()
    }else if(e.detail.value==1){
      this.setData({
        isLu:true
      })
      this.initType1()
    }else if(e.detail.value==2){
      this.setData({
        isKd:true
      })
      this.initType2()
    }
  },
  bindXQChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      xq: this.data.xqList[e.detail.value],
      isDong:true
    })
  },
  bindDongChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      dong: this.data.dongList[e.detail.value],
      isShi:true
    })
    console.log('shi',this.data.isShi)
  },
  bindShiChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      shi: this.data.shiList[e.detail.value]
    })
  },
  bindLuChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      lu: this.data.luList[e.detail.value],
      isHao:true
    })
  },
  bindHaoChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      hao: this.data.haoList[e.detail.value],
    })
  },
  bindKdChange: function(e) {
    console.log('e.detail.value',e.detail.value)
    this.setData({
      kd: this.data.kdList[e.detail.value],
    })
  },
  kdInput(e){
    this.setData({kd:e.detail.value})
  },
  checkTip(e){
    var name = e.currentTarget.dataset.name;
    console.log(name);
    let ndata=this.data.list[name].ids;
    wx.setStorageSync('nData', ndata);
    wx.navigateTo({
      url: '/pages/recordDetail/recordDetail',
    });
  }
})
